package com.haiyu.manager.controller.platform;

import com.haiyu.manager.pojo.Worker;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.WorkerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("worker")//指定路由
public class WorkerController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    WorkerService workerService;
    /**
     *
     * 功能描述: 分页查询worker列表
     *
     *
     */
    @RequestMapping(value = "/getWorkers", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult getWorkers(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize, @RequestParam("buyerOpenid") String openId){

        System.out.println(openId);
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取用户列表
            pdr = workerService.getWorkers(openId,0, pageNum ,pageSize);
            logger.info("Workers列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Workers列表查询异常！", e);
        }
        return pdr;

    }

    /**
     *
     *述: 设置[更新]
     *
     *
     */
    @PostMapping("setWorker")
    @ResponseBody
    public Map<String,Object> setWorker(Worker worker) {
        logger.info("设置worker[新增或更新]！worker:" + worker);
        Map<String,Object> data = new HashMap();
        if(worker.getId() == null){


        }else{
            //修改
            data = workerService.updateWorker(worker);
        }
        return data;
    }


    /**
     *
     * 功能描述: 删除/恢复
     *
     */
    @PostMapping("updateWorkerIsAllowed")
    @ResponseBody
    public Map<String,Object> updateWorkerIsAllowed(@RequestParam("id") int id,@RequestParam("isAllowed") Integer isAllowed) {
        logger.info("删除/恢复！id:" + id+" isAllowed:"+isAllowed);
        Map<String, Object> data = new HashMap<>();
        if(isAllowed == 1){
            //删除
            data = workerService.delWorker(id,isAllowed);
        }else{
            //恢复
            data = workerService.recoverWorker(id,isAllowed);
        }
        return data;
    }

}
